home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / oobr / tree-x / usleep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-05  |  2.2 KB  |  72 lines

  1. /*
  2.  *  NAME:
  3.  *      usleep     -- This is the precision timer for Test Set
  4.  *                    Automation. It uses the select(2) system
  5.  *                    call to delay for the desired number of
  6.  *                    micro-seconds. This call returns ZERO
  7.  *                    (which is usually ignored) on successful
  8.  *                    completion, -1 otherwise. 
  9.  *
  10.  *  ALGORITHM:
  11.  *      1) We range check the passed in microseconds and log a
  12.  *         warning message if appropriate. We then return without
  13.  *         delay, flagging an error. 
  14.  *      2) Load the Seconds and micro-seconds portion of the
  15.  *         interval timer structure.
  16.  *      3) Call select(2) with no file descriptors set, just the
  17.  *         timer, this results in either delaying the proper
  18.  *         ammount of time or being interupted early by a signal.
  19.  *
  20.  *  HISTORY:
  21.  *      Added when the need for a subsecond timer was evident.
  22.  *
  23.  *  AUTHOR:
  24.  *      Michael J. Dyer   <mike@sherlock.med.ge.com>
  25.  */
  26.  
  27. #ifndef HAVE_USLEEP
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <time.h>
  33. #include <sys/time.h>
  34. #include <sys/param.h>
  35. #include <sys/types.h>
  36.  
  37. int     usleep( unsigned long int microSeconds )
  38. {
  39.         unsigned int            Seconds, uSec;
  40.         int                     nfds, readfds, writefds, exceptfds;
  41.         struct  timeval         Timer;
  42.  
  43.         nfds = readfds = writefds = exceptfds = 0;
  44.  
  45.         if( (microSeconds == (unsigned long) 0) 
  46.                 || microSeconds > (unsigned long) 4000000 )
  47.         {
  48.                 errno = ERANGE;         /* value out of range */
  49.                 perror( "usleep time out of range ( 0 -> 4000000 ) " );
  50.                 return -1;
  51.         }
  52.  
  53.         Seconds = microSeconds / (unsigned long) 1000000;
  54.         uSec    = microSeconds % (unsigned long) 1000000;
  55.  
  56.         Timer.tv_sec            = Seconds;
  57.         Timer.tv_usec           = uSec;
  58.  
  59.         if( select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0 )
  60.         {
  61.                 perror( "usleep (select) failed" );
  62.                 return -1;
  63.         }
  64.  
  65.         return 0;
  66. }
  67. #else
  68. /* this is to avoid a "object file has no symbol" error/warning.
  69. */
  70. static int local_junk;
  71. #endif
  72.